package com.lywz.web.action.admin.food;

import java.io.File;
import java.util.List;

import com.lywz.biz.common.entity.goodfood.SpecialSnack;
import com.lywz.core.exception.SSException;
import com.lywz.core.fileUpload.UploadFile;
import com.lywz.core.log.LogClerk;
import com.lywz.core.web.WebConstants;
import com.lywz.web.struts.GZAbstracterAction;

/**
 * @author qianqi yangbolin
 * @date 2010-5-7 特色情小吃的后台管理action
 */
public class SpecialSnackBackOperateAction extends GZAbstracterAction {

    private static final long serialVersionUID = -3338435632100251247L;

    private SpecialSnack specialSnack;

    private List<SpecialSnack> specialSnackList;

    private int specialSnackId;

    //图像文件
    private File imageFile;

    //文件名称
    private String imageFileFileName;

    /**
     * 利用权值 获得特色小吃的全部的列表
     * 
     * @return
     */
    public String getSpecialFoodListOrdreByPower() {
        try {
            specialSnackList = specialSnackService.getAllSpecialSnackListByPower();
            return "toSpecialFoodList";
        } catch (Exception e) {
            LogClerk.sysout.error(e);
            return ERROR;
        }
    }

    /**
     * 刪除一個特色小吃的个体
     * 
     * @return
     */
    public String deleteOneSpecialSnack() {
        try {
            if (specialSnackService.delteOneSpecialSnackById(specialSnackId)) return "actionToSpecialFoodList";
            else return ERROR;
        } catch (Exception e) {
            LogClerk.sysout.error(e);
            return ERROR;
        }
    }

    /**
     * 添加一个美食
     * 
     * @return
     */
    public String addOneSpecialSnack() {

        String imageUrl = "";
        try {
            imageUrl = saveImageFile(imageFile, WebConstants.foodImagePath, imageFileFileName, 0, 0);
        } catch (Exception e) {
            LogClerk.sysout.error(e);
            return ERROR;
        }

        specialSnack.setImageUrl(imageUrl);

        try {
            if (specialSnackService.addOneSnack(specialSnack)) return "actionToSpecialFoodList";
            else return ERROR;

        } catch (Exception e) {
            LogClerk.sysout.error(e);
            return ERROR;
        }
    }

    /**
     * 修改美食
     * @return
     */
    public String updateOneSpecialSnack() {
        String imageUrl = "";
        try {
            imageUrl = saveImageFile(imageFile, WebConstants.foodImagePath, imageFileFileName, 0, 0);
        } catch (Exception e) {
            LogClerk.sysout.error(e);
            return ERROR;
        }

        specialSnack.setImageUrl(imageUrl);
        
        try {
            if (specialSnackService.updateOneSnack(specialSnack)) return "actionToSpecialFoodList";
            else return ERROR;

        } catch (Exception e) {
            LogClerk.sysout.error(e);
            return ERROR;
        }
    }

    /**
     * 保存上传图片并返回路径
     * 
     * @param imageFile
     * @param dir
     * @param imageFileFileName
     * @param imageWith
     * @param imageHeight
     * @return
     * @throws SSException
     */
    public String saveImageFile(File imageFile, String dir, String imageFileFileName,
            int imageWith, int imageHeight) throws SSException {

        UploadFile files = new UploadFile(imageFile, dir, imageFileFileName, imageWith,
                imageHeight, request);

        String path = defaultFileUpload.uploadOneFile(files);

        return path;
    }

    /**
     * 跳转到添加页面
     * 
     * @return
     */
    public String toAddSnack() {
        return "goToAddFoodPage";
    }

    /**
     * 跳转到修改页面
     * 
     * @return
     */
    public String toUpdateSnack() {
        try {
            specialSnack = specialSnackService.getOneSpecialSnack(specialSnackId);
            return "goToUpdateSnackPage";
        } catch (SSException e) {
            LogClerk.sysout.error(e);
            return ERROR;
        }
    }
    
    /**
     * 跳转到特色小食的单个显示页面前 对该特色小食个体 实例
     * @return
     */
    public String toShowSnack(){
        try{
            specialSnack = specialSnackService.getOneSpecialSnack(specialSnackId);
            return "goToShowSpecialSnack";
        }catch (Exception e) {
            LogClerk.sysout.error(e);
            return ERROR;
        }
    }

    ///////////$$$$$$$$$$$$getter and setter$$$$$$$$$$$$$$$$$$$//////////////
    /**
     * @return the specialSnack
     */
    public SpecialSnack getSpecialSnack() {
        return specialSnack;
    }

    /**
     * @return the specialSnackList
     */
    public List<SpecialSnack> getSpecialSnackList() {
        return specialSnackList;
    }

    /**
     * @return the specialSnackId
     */
    public int getSpecialSnackId() {
        return specialSnackId;
    }

    /**
     * @param specialSnack the specialSnack to set
     */
    public void setSpecialSnack(SpecialSnack specialSnack) {
        this.specialSnack = specialSnack;
    }

    /**
     * @param specialSnackId the specialSnackId to set
     */
    public void setSpecialSnackId(int specialSnackId) {
        this.specialSnackId = specialSnackId;
    }

    /**
     * @return the imageFile
     */
    public File getImageFile() {
        return imageFile;
    }

    /**
     * @return the imageFileFileName
     */
    public String getImageFileFileName() {
        return imageFileFileName;
    }

    /**
     * @param imageFile the imageFile to set
     */
    public void setImageFile(File imageFile) {
        this.imageFile = imageFile;
    }

    /**
     * @param imageFileFileName the imageFileFileName to set
     */
    public void setImageFileFileName(String imageFileFileName) {
        this.imageFileFileName = imageFileFileName;
    }

}
